#include "05_Producer.hpp"
#include "06_Consumer.hpp"
#include <vector>

using std::vector;

void test(){
    printf("main | 开始创建TaskQueue对象\n");
    TaskQueue taskque(10, 8);
    
    printf("main | 开始创建Producer与Consumer对象\n");
    vector<Producer> producers;
    vector<Consumer> consumers;
    for(int i = 0; i < 3; ++i)
    {
        //Producer(&taskque);
        //producers.emplace_back(&taskque);
        producers.emplace_back(taskque);
    }
    for(int i = 0; i < 2; ++i)
    {
        consumers.emplace_back(taskque);
    }

    printf("main | 开始运行Producer与Consumer对象\n");
#if 0
    // 串行执行，消费者会被阻塞，看起来不怎么消费，非常地节俭
    for(auto& producer : producers)
    {
        producer.run();
    }
    for(auto& consumer : consumers)
    {
        consumer.run();
    }

#endif

    for(auto& producer : producers)
    {
        producer.start();
    }
    for(auto& consumer : consumers)
    {
        consumer.start();
    }

    // 记得写join
    for(auto& producer : producers)
    {
        producer.join();
    }
    for(auto& consumer : consumers)
    {
        consumer.join();
    }
}

int main()
{
    test();
    return 0;
}

